﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Atrax.Contracts;
using Atrax.Library;

namespace Atrax.Web.Dispatcher
{
	internal static class ResponseFactory
	{
		internal static string GetMethodNotSupportedResponse(string method)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
			sb.AppendLine("<ExecuteResponse>");
			sb.AppendLine("  <ExecuteResult>");
			sb.AppendLine("    <QueryId></QueryId>");
			sb.AppendLine("    <StatusCode>501</StatusCode>");
			sb.AppendLine("    <StatusDescription>" + method + " not supported</StatusDescription>");
			sb.AppendLine("    <EstimatedDeliverySeconds></EstimatedDeliverySeconds>");
			sb.AppendLine("    <ValidationResult>");
			sb.AppendLine("      <IsValid>false</IsValid>");
			sb.AppendLine("      <ValidationReport>0</ValidationReport>");
			sb.AppendLine("    </ValidationResult>");
			sb.AppendLine("  </ExecuteResult>");
			sb.AppendLine("</ExecuteResponse>");
			return sb.ToString();
		}

		internal static string GetQueryResponse(QueryResponse response)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
			sb.AppendLine("<ExecuteResponse>");
			sb.AppendLine("  <ExecuteResult>");
			sb.AppendLine("    <QueryId>" + response.QueryId + "</QueryId>");
			sb.AppendLine("    <StatusCode>" + response.StatusCode + "</StatusCode>");
			sb.AppendLine("    <StatusDescription>" + response.StatusDescription + "</StatusDescription>");
			sb.AppendLine("    <EstimatedDeliverySeconds>" + response.EstimatedDeliverySeconds.ToString() + "</EstimatedDeliverySeconds>");
			sb.AppendLine("    <ValidationResult>");
			if (response.ValidationResult != null)
			{
				sb.AppendLine("      <IsValid>" + response.ValidationResult.IsValid.ToString() + "</IsValid>");
				sb.AppendLine("      <ValidationReport>" + response.ValidationResult.ValidationReport.ToXmlString() + "</ValidationReport>");
			}
			else
			{
				sb.AppendLine("      <IsValid></IsValid>");
				sb.AppendLine("      <ValidationReport></ValidationReport>");
			}
			sb.AppendLine("    </ValidationResult>");

			if (response.SampleRequest != null)
			{
				sb.AppendLine("    <SampleRequest>" + response.SampleRequest.ToXmlString() + "</SampleRequest>");
			}

			sb.AppendLine("  </ExecuteResult>");
			sb.AppendLine("</ExecuteResponse>");
			return sb.ToString();
		}

		internal static string GetMissingValues()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
			sb.AppendLine("<ExecuteResponse>");
			sb.AppendLine("  <ExecuteResult>");
			sb.AppendLine("    <QueryId></QueryId>");
			sb.AppendLine("    <StatusCode>502</StatusCode>");
			sb.AppendLine("    <StatusDescription>Missing one or more required values. See application documentation.</StatusDescription>");
			sb.AppendLine("    <EstimatedDeliverySeconds>0</EstimatedDeliverySeconds>");
			sb.AppendLine("    <ValidationResult>");
			sb.AppendLine("      <IsValid>false</IsValid>");
			sb.AppendLine("      <ValidationReport></ValidationReport>");
			sb.AppendLine("    </ValidationResult>");
			sb.AppendLine("  </ExecuteResult>");
			sb.AppendLine("</ExecuteResponse>");
			return sb.ToString();
		}

		internal static string GetException(Exception e)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
			sb.AppendLine("<ExecuteResponse>");
			sb.AppendLine("  <ExecuteResult>");
			sb.AppendLine("    <QueryId></QueryId>");
			sb.AppendLine("    <StatusCode>503</StatusCode>");
			sb.AppendLine("    <StatusDescription>" + e.ToString().ToXmlString() + "</StatusDescription>");
			sb.AppendLine("    <EstimatedDeliverySeconds>0</EstimatedDeliverySeconds>");
			sb.AppendLine("    <ValidationResult>");
			sb.AppendLine("      <IsValid>false</IsValid>");
			sb.AppendLine("      <ValidationReport></ValidationReport>");
			sb.AppendLine("    </ValidationResult>");
			sb.AppendLine("  </ExecuteResult>");
			sb.AppendLine("</ExecuteResponse>");
			return sb.ToString();
		}
	}
}
